package org.example.completed.controller;


import org.example.completed.domain.PaideventReservation;
import org.example.completed.model.vo.ResultVO;
import org.example.completed.service.PaideventReservationService;
import org.example.completed.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 *  付费小活动
 *
 * @since 2025-04-12
 */
@RestController
@RequestMapping("/paideventReservation")
public class PaideventReservationController {

    @Autowired
    private PaideventReservationService paideventReservationService;

    @Autowired
    private UserService userService;



    /**
     * 用户报名参观里面小活动
     * @param userId 用户ID
     * @return 报名结果
     */
    @PostMapping("/register")
    public ResultVO<String> register(@RequestParam String userId, @RequestParam String contact) {
        try {
            paideventReservationService.register(userId, contact);
            return ResultVO.ok("报名成功");
        } catch (IllegalArgumentException e) {
            return ResultVO.fail(e.getMessage());
        }
    }

    /**
     * 新增付费活动预约信息
     * @param reservation 付费活动预约实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public ResultVO<String> addReservation(@RequestBody PaideventReservation reservation) {
        try {
            paideventReservationService.save(reservation);
            return ResultVO.ok("新增成功");
        } catch (Exception e) {
            return ResultVO.fail("新增失败: " + e.getMessage());
        }
    }

    /**
     * 删除付费活动预约信息
     * @param id 付费活动预约ID
     * @return 删除结果
     */
    @PostMapping("/delete/{id}")
    public ResultVO<String> deleteReservation(@PathVariable String id) {
        try {
            paideventReservationService.removeById(id);
            return ResultVO.ok("删除成功");
        } catch (Exception e) {
            return ResultVO.fail("删除失败: " + e.getMessage());
        }
    }

    /**
     * 修改付费活动预约信息
     * @param reservation 付费活动预约实体
     * @return 修改结果
     */
    @PostMapping("/update")
    public ResultVO<String> updateReservation(@RequestBody PaideventReservation reservation) {
        try {
            paideventReservationService.updateById(reservation);
            return ResultVO.ok("修改成功");
        } catch (Exception e) {
            return ResultVO.fail("修改失败: " + e.getMessage());
        }
    }

    /**
     * 查询单个付费活动预约信息
     * @param id 付费活动预约ID
     * @return 付费活动预约信息
     */
    @GetMapping("/get/{id}")
    public ResultVO<PaideventReservation> getReservation(@PathVariable String id) {
        try {
            PaideventReservation reservation = paideventReservationService.getById(id);
            return ResultVO.ok(reservation);
        } catch (Exception e) {
            return ResultVO.fail("查询失败: " + e.getMessage());
        }
    }

    /**
     * 查询所有付费活动预约信息
     * @return 所有付费活动预约信息列表
     */
    @GetMapping("/list")
    public ResultVO<List<PaideventReservation>> listReservations() {
        try {
            List<PaideventReservation> reservations = paideventReservationService.list();
            return ResultVO.ok(reservations);
        } catch (Exception e) {
            return ResultVO.fail("查询失败: " + e.getMessage());
        }
    }

    /**
     * 修改小活动价格标价
     * @param newPrice 新的价格
     * @return 修改结果
     */
    @PostMapping("/updatePrice")
    public ResultVO<String> updatePriceForIdOne(@RequestParam BigDecimal newPrice) {
        try {
            PaideventReservation reservation = paideventReservationService.getById("1");
            if (reservation != null) {
                reservation.setPrice(newPrice);
                paideventReservationService.updateById(reservation);
                return ResultVO.ok("价格修改成功");
            } else {
                return ResultVO.fail("未找到记录");
            }
        } catch (Exception e) {
            return ResultVO.fail("修改失败: " + e.getMessage());
        }
    }
}

